/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine;

import java.io.InputStream;
import java.util.Scanner;

public abstract class GameLoader {

    public String engineClass, version, spriteIcon, icon, name, desc;
    public double engineVerMin, engineVerMax;

    public abstract InputStream getFile(String fileName);

    protected void loadInfo() {
        Scanner in = new Scanner(getFile("info"));
        while (in.hasNextLine()) {
            String line = in.nextLine();
            String[] tokens = line.split(" ", 2);
            if ("version".equals(tokens[0]))
                version = tokens[1];
            else if ("engineClass".equals(tokens[0]))
                engineClass= tokens[1];
            else if ("requires".equals(tokens[0])) {
                Log.warn("requires is deprecated");
                double required = Double.parseDouble(tokens[1]);
                if (Math.abs(required - Main.ENGINE_VERSION) > 0.00001)
                    Log.warn("Game requires version '%.4f' but engine version is %.4f",
                            required, Main.ENGINE_VERSION);
            } else if ("spriteIcon".equals(tokens[0]))
                spriteIcon = tokens[1];
            else if ("icon".equals(tokens[0]))
                icon = tokens[1];
            else if ("name".equals(tokens[0]))
                name = tokens[1];
            else if ("desc".equals(tokens[0]))
                desc = tokens[1];
            else if ("madeForEngineVer".equals(tokens[0])) {
                String[] tokenz = tokens[1].split("-");
                engineVerMin = Double.parseDouble(tokenz[0]);
                if (tokenz.length > 1)
                    engineVerMax = Double.parseDouble(tokenz[1]);
            } else
                Log.warn("Unknown token '%s' in the info for game", tokens[0]);
        }
    }
}
